import {Injectable} from '@angular/core';
import {HttpService} from '@kylin/http';

@Injectable()
export class DrFeaService {
    ALL: any[] = [];
    all: any[] = [];
    aa = true;
    constructor(private http: HttpService) {
    }

    getALL(type, json, name) {
        return new Promise((resolve) => {
            const Attr: any = {};
            Attr.layerType = type; // 1：画的点; 2：画的线; 3：画的面; 4：船点; 5：船线; 6：船面
            Attr.layerName = name;
            Attr.layerJson = json; // layerType图层类型为1/2/3时的图层数据
            this.ALL.push(Attr);
            resolve(this.ALL)
        })
    }

    getall(type, json, name) {
        return new Promise((resolve) => {
            this.getALL(type, json, name).then((Geodata: any) => {
                this.all = []
                Geodata.map(i => {
                    const aa: any = {};
                    aa.name = i.layerName;
                    aa.type = i.layerType;
                    if (i.id) {
                        aa.id = i.id
                    }
                    aa.Ljson = [];
                    if (i.layerJson.length !== undefined) {
                        i.layerJson.features.map(v => {
                            const fea: any = {};
                            fea.id = v.id;
                            fea.name = type;
                            aa.Ljson.push(fea);
                        });
                    }
                    this.all.push(aa);
                });
                resolve([this.ALL, this.all])
            })
        })
    }

    addNew(layer) {
        return this.http.post('/layer', {layer: layer})
    }

    judgePointLayers(layersDataList: any[], drawType) {
        this.aa = true;
        layersDataList.map(data => {
            if (data.layerType === drawType) {
                this.aa = false;
            }
        });
        return this.aa;
    }
}
